<?php
/*
Plugin Name: Omeka Addons Directory
Plugin URI: http://omeka.org
Description: Plugin to manage Omeka plugins and themes from the Addons repository.
Author: Center for History and New Media
Version: 1.0
Author URI: http://omeka.org
*/

include('forms.php');
include('helpers.php');
include('routes.php');

// define global variable for the plugins path
define(OMEKA_ADDONS_PATH , ABSPATH . PLUGINDIR . DIRECTORY_SEPARATOR . 'omeka-addons' . DIRECTORY_SEPARATOR);

define(OMEKA_ADDONS_SVN_PATH, 'https://addons.omeka.org/svn/');

define(OMEKA_ADDONS_ZIP_FILES_PATH, '/websites/omeka.org/files/');

define(OMEKA_ADDONS_BASE_URL, 'add-ons');

// When the plugin is activated, install it, add database tables and other craziness.
if (isset($_GET['activate']) && $_GET['activate'] == 'true')
{
	add_action('init', 'omeka_addons_install');
}

function omeka_addons_install()
{
    global $wpdb;
	// Declare table names
	$addons_table_name = $wpdb->prefix . "addons";
	$versions_table_name = $wpdb->prefix . "versions";
	$addons_tags_table_name = $wpdb->prefix . "addons_tags";
	$addons_taggings_table_name = $wpdb->prefix . "addons_taggings";

	// First-Run-Only parameters: Check if addons table exists:
	if($wpdb->get_var("SHOW TABLES LIKE '$addons_table_name'") != $addons_table_name) 
	{
	    // It doesn't exist, create the table
		$sql = "CREATE TABLE `" . $addons_table_name . "` (
			  `id` int(11) NOT NULL AUTO_INCREMENT,
			  `directory_name` varchar(255) NOT NULL,
			  `type` varchar(255) NOT NULL,
			  `featured` tinyint(1) NOT NULL,
			  `addon_name` varchar(255) NULL,
			  `addon_description` mediumtext NULL,
			  PRIMARY KEY (`id`)
			)";

	    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	    dbDelta($sql);
	}

	// First-Run-Only parameters: Check if addons table exists:
	if($wpdb->get_var("SHOW TABLES LIKE '$versions_table_name'") != $versions_table_name) 
	{
		//It doesn't exist, create the table
		$sql = "CREATE TABLE `" . $versions_table_name . "` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `addon_id` int(11) NOT NULL,
		  `name` varchar(255) default NULL,
		  `author` varchar(255) default NULL,
		  `version` varchar(255) default NULL,
		  `description` mediumtext,
		  `link` varchar(255) default NULL,
		  `omeka_minimum_version` varchar(255) default NULL,
		  `omeka_tested_up_to` varchar(255) default NULL,
		  `tag_directory` varchar(255) NOT NULL,
		  PRIMARY KEY (`id`)
		)";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	}

	// First-Run-Only parameters: Check if addons table exists:
	if($wpdb->get_var("SHOW TABLES LIKE '$addons_tags_table_name'") != $addons_tags_table_name) 
	{	
		//It doesn't exist, create the table
		$sql = "CREATE TABLE `" . $addons_tags_table_name . "` (
		  `id` int(10) unsigned NOT NULL auto_increment,
		  `name` varchar(255) collate utf8_unicode_ci default NULL,
		  PRIMARY KEY  (`id`),
		  UNIQUE KEY `name` (`name`)
		)";
		
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	}

	// First-Run-Only parameters: Check if addons table exists:
	if($wpdb->get_var("SHOW TABLES LIKE '$addons_taggings_table_name'") != $addons_taggings_table_name) 
	{	
		//It doesn't exist, create the table
		$sql = "CREATE TABLE `" . $addons_taggings_table_name . "` (
		  `id` int(10) unsigned NOT NULL auto_increment,
		  `addon_id` int(10) unsigned NOT NULL,
		  `tag_id` int(10) unsigned NOT NULL,
		  `type` varchar(50) collate utf8_unicode_ci NOT NULL default '',
		  PRIMARY KEY  (`id`)
		)";
		
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	}
}

function omeka_addons_admin_menu()
{
 add_menu_page('Dashboard','Omeka Plugins',8,'omeka-plugins','omeka_addons_manage_plugins');
 add_menu_page('Dashboard','Omeka Themes',8,'omeka-themes','omeka_addons_manage_themes');

}

add_action('admin_menu', 'omeka_addons_admin_menu');


function omeka_addons_manage_themes()
{
    return omeka_addons_manage_form('theme');
}

function omeka_addons_manage_plugins()
{
    return omeka_addons_manage_form('plugin');
}